<?php
/**
 * Class Lib_infoAes
 */

class Lib_infoAes
{
    private static $obj = null;
    private static $key = 'WHZ5Xg2JZhXtcRxl';
    private static $iv = 'nfxm0Iyb6Ub2WpLT';
    private static $method = 'AES-256-CBC';
    /**
     * 获取单例对象
     * @param string $hl
     * @param number $timeout
     * @return Lib_Request_Aes
     */
    public static function getInstance()
    {
        if (is_null(self::$obj)) {
            self::$obj = new self();
        }
        return self::$obj;
    }


    /**
     * @desc AES加密
     * @param $data
     * @return string
     */
    public function aesEncrypt($data)
    {
        if(empty($data)){
            return $data;
        }
        if(is_array($data)){
            $data = json_encode($data);
        }
        return base64_encode(openssl_encrypt($data, self::$method, self::$key, 1, self::$iv));
    }

    /**
     * @desc AES解密
     * @param $data
     * @return string
     */
    public function aesDecrypt($data)
    {
        if(empty($data)){
            return $data;
        }
        return openssl_decrypt(base64_decode($data), self::$method, self::$key, true, self::$iv);
    }

}